//评论
(function(){
	var config = require("../_include/config");
	var db = require("../db/index");
	var db_table_name = config.dbname.comment;
	var db_id_prefix = config.dbIdPrefix.comment;
	//构造函数
	var _con = function (info){
		var self = this;
		//编号
		self._id = db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16);
		//评论者类型
		self.type = info.type;

		//评论者对象
		var Constructor = require("../"+info.type+"/_con");
		console.log(info.commenter_id,info.type);
		if (Constructor) {
			var commenter = Constructor.get(info.commenter_id);
			if (commenter) {
				self.commenter_id = info.commenter_id;
			}else{
				throwCusError("找不到指定评论者");
			}
		}else{
			throwCusError("找不到指定类");
		}

		//评论内容
		if (info.content = info.content.trim()) {
			self.content = info.content;
		}else {
			throwCusError("评论内容不能为空")
		}

		//回复某条评论
		if (info.reply_id) {
			var replyComment = _con.get(info.reply_id);
			if(replyComment){
				self.reply_id = info.reply_id;
			}else{
				throwCusError("找不到指定评论")
			}
		}
		//子评论
		self.comments = [];
		//是否匿名（匿名这个功能仅限于用户）?还是不提供
		self.isAny = info.isAny?(String(info.isAny)==="false"?true:false):false;
		//是否属于评价，是的话为评价的ID
		self.evaluation_id = info.evaluation_id;
		//创建时间
		self.create_time = +new Date;
		db.insert(db_table_name,self);

		//直接注入到关联者的数据中
		commenter&&commenter._addComment(self._id);
		replyComment&&replyComment._addComment(self._id);

		console.log("生成一个评论：",self._id);
	};
	//技能
	_con.prototype = require("./skill");
	module.exports = _con;
	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
	
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../user/_con");
	var BusCon = require("../bus/_con");
}());